/**   
 * @Title: AuthUtils.java 
 * @author 7828wc   
 * @date 2015年9月14日 下午3:16:34 
 */
package com.onstar.cn.api.util;

import java.security.InvalidKeyException;
import java.security.SignatureException;

import com.gm.onstar.sdk.errors.RemoteApiAuthHandlingError;
import com.gm.onstar.sdk.live.LiveAuthSDKJsonHelper;
import com.gm.onstar.sdk.response.IDToken;

/**
 * @ClassName: AuthUtils.java
 * @Description: Onstar API 工具类
 * @author 7828wc
 * @date 2015年9月14日
 */
public class AuthUtils {
	/**
	 * 输入密码进行获取Access-token和IDTOKEN
	 * @param username
	 * @param password
	 * @param clientId
	 * @param deviceId
	 * @param clientSecret
	 * @param scope
	 * @return
	 * @throws InvalidKeyException
	 * @throws SignatureException
	 */
	public static String encodeForInitialAuth(String username, String password, String clientId, String deviceId,
			String clientSecret, String scope) throws InvalidKeyException, SignatureException {
		LiveAuthSDKJsonHelper authHelper = new LiveAuthSDKJsonHelper();
		
		return authHelper.encodeForInitialAuth(username, password, clientId, deviceId, clientSecret, scope);
	}
	
	/**
	 * Access token过期，通过IDTOKEN刷新Access token
	 * @param clientId
	 * @param idToken
	 * @param deviceId
	 * @param clientSecret
	 * @param scope
	 * @return
	 * @throws RemoteApiAuthHandlingError
	 * @throws InvalidKeyException
	 * @throws SignatureException
	 */
	public static String encodeForReauthentication(String clientId, String idToken, String deviceId,
			String clientSecret,  String scope)
					throws RemoteApiAuthHandlingError, InvalidKeyException, SignatureException {
		LiveAuthSDKJsonHelper authHelper = new LiveAuthSDKJsonHelper();
		IDToken idtoken = new IDToken(idToken);
		return authHelper.encodeForReauthentication(clientId, idtoken, deviceId, clientSecret, scope);
	}

	/**
	 * 解析JWT字符串为Json
	 * @param json
	 * @return
	 * @throws RemoteApiAuthHandlingError
	 */
	public static String decodeJwtToJson(String json) throws RemoteApiAuthHandlingError {
		LiveAuthSDKJsonHelper authHelper = new LiveAuthSDKJsonHelper();
		String response = authHelper.decodeJwtToJsonString(json);
		return response;
	}

	/**
	 * 输入PIN进行获取Access-token和IDTOKEN
	 * @param clientId
	 * @param idToken
	 * @param deviceId
	 * @param clientSecret
	 * @param pin
	 * @param scope
	 * @return
	 * @throws RemoteApiAuthHandlingError
	 * @throws InvalidKeyException
	 * @throws SignatureException
	 */
	public static String encodeForPinReauthentication(String clientId, String idToken, String deviceId,
			String clientSecret, String pin, String scope)
					throws RemoteApiAuthHandlingError, InvalidKeyException, SignatureException {
		LiveAuthSDKJsonHelper authHelper = new LiveAuthSDKJsonHelper();
		IDToken idtoken = new IDToken(idToken);
		return authHelper.encodeForPinReauthentication(clientId, idtoken, deviceId, clientSecret, pin, scope);
	}

	/**
	 * Access token过期，通过IDTOKEN和PIN刷新Access token
	 * @param clientId
	 * @param deviceId
	 * @param clientSecret
	 * @param pin
	 * @return
	 * @throws InvalidKeyException
	 * @throws SignatureException
	 */
	public static String encodeForUpgradeToken(String clientId, String deviceId, String clientSecret, String pin)
			throws InvalidKeyException, SignatureException {
		LiveAuthSDKJsonHelper authHelper = new LiveAuthSDKJsonHelper();
		return authHelper.encodeForUpgradeToken(clientId, deviceId, clientSecret, pin);
	}
}
